<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Manage\AdminManage;


class BaseController extends Controller
{

    protected $model;
    protected $error;


    // 初始化
    public function initialize()
    {
        // 权限判断 通过控制器来判断
        $option     = $this->optLogic();
        $action     = $option->getData('action', null, 'trim');
        $controller = $option->getData('controller', null, 'trim');
        $controller = strtolower($controller);

        // 获取用户信息
        //  登录。获取token的时候，不需要登录
        $user = [];
        if(($controller == 'account') && (in_array($action, ['login', 'token', 'captcha', 'click', 'checkClick']))){

        } else{
            $user = $this->myUser();
        }


        // 所有的人都可以操作的控制器：测试，登录，
        if(!in_array($controller, ['account', 'test', 'frontend', 'deal'])){
            //$option->throwIf(empty($user), '请登录后在操作！！', LOGIN_ERROR);
            $option->throwIf(empty($user), 'please_login_before_operation', LOGIN_ERROR);

            // TODO 公共权限
            //   发布文章快讯新鲜事：可以选择用户，选择话题
            $con_act = $controller.'_'.$action;
            $hasRole = in_array($con_act, ['user_lists', 'topic_lists',]);

            // 判断权限：是否含有当前控制器 => 如果不存在 则没有权限
            // 如果是1 超级管理员 所有权限都有
            if(($user['uid'] != 1) || empty($hasRole)){
                // 权限处理
                /*// $role = $user['role'] ?? '[]';
                // $role = json_decode($role, true);
                $role = array_column($user['role'], 'role');

                // 其中几个特殊控制器
                $all         = [];
                $controllers = ['point', 'business', 'dispatch', 'abroad', 'news', 'author', 'image',];
                foreach($user['role'] as $item){
                    $_role = $item['role'];
                    if(in_array($_role, $controllers)){
                        $all[] = $_role.'-cate';
                        $all[] = $_role.'-article';
                        $all[] = 'comment';
                    }
                    // 栏目中，获取文章列表
                    if($_role == 'cate'){
                        $all[] = 'article';
                    }
                    $all[] = $_role;
                }

                $this->throwIf(!in_array($controller, $all), '你没有该操作权限！！');*/
            }
            /*if($user['uid'] == 1){
                $this->throwIf(!$user['hasRole'], '你没有该操作权限！！');
            }*/

        }
    }

    // 获取用户以及权限
    protected function myUser()
    {
        $option = $this->optLogic();
        $info   = $option->token('back-token');
        if($info === false){
            $option->throwIf(empty($user), $option->getError(), $option->getCode());
        }
        if(empty($info)){
            return [];
        }
        // 组合
        $info['uid']  = $info['id'];
        $this->myUid  = $info['id'];
        $this->myUser = $info;

        return $info;
    }


    ######################################################################################################
    ## 请求

    /**
     * // 返回列表数据
     * @var  $logic \App\Manage\Manage
     */
    public function onLists($logic, $options = [])
    {
        // 参数
        if(empty($options)){
            $options = ['is_admin_log' => 0, 'is_admin_role' => 0,];
        }

        // 执行
        $result = $logic->lists($this->myUser, $options);
        if($result === false){
            return $this->error($logic->getError());
        }

        return $this->success($result);
    }
    /**
     * // 返回列表数据
     * @var  $logic \App\Manage\Manage
     */
    public function onAll($logic, $options = [])
    {
        // 参数
        if(empty($options)){
            $options = ['is_admin_log' => 0, 'is_admin_role' => 0,];
        }

        // 执行
        $result = $logic->all($this->myUser, $options);
        if($result === false){
            return $this->error($logic->getError());
        }

        return $this->success($result);
    }

    /**
     * // 返回详情数据
     * @var  $logic \App\Manage\Manage
     */
    public function onShow($logic, $options = [])
    {
        // 参数
        if(empty($options)){
            $options = ['is_admin_log' => 0, 'is_admin_role' => 0,];
        }

        // 执行
        $info = $logic->show($this->myUser, $options);
        if($info === false){
            return $this->error($logic->getError());
        }

        return $this->success($info);
    }

    /**
     * // 删除操作
     * @var  $logic \App\Manage\Manage
     */
    public function onDel($logic, $options = [])
    {
        // 参数
        if(empty($options)){
            $options = ['is_admin_log' => 0, 'is_admin_role' => 0,];
        }

        // 执行
        $res = $logic->del($this->myUser, $options);
        if($res === false){
            return $this->error($logic->getError());
        }

        return $this->success();
    }

    /**
     * // 删除操作
     * @var  $logic \App\Manage\Manage
     */
    public function onSoft($logic, $field='status',$options = [])
    {
        // 参数
        if(empty($options)){
            $options = ['is_admin_log' => 0, 'is_admin_role' => 0,];
        }

        // 执行
        $res = $logic->softDel($field, $this->myUser, $options);
        if($res === false){
            return $this->error($logic->getError());
        }

        return $this->success();
    }


    /**
     * // 添加操作
     * @var  $logic \App\Manage\Manage
     */
    public function onAdd($logic, $options = [])
    {
        // 参数
        if(empty($options)){
            $options = ['is_admin_log' => 0, 'is_admin_role' => 0,];
        }

        // 执行
        $res = $logic->add($this->myUser, $options);
        if($res === false){
            return $this->error($logic->getError());
        }

        return $this->success();
    }


    /**
     * // 修改操作
     * @var  $logic \App\Manage\Manage
     */
    public function onEdit($logic, $options = [])
    {
        // 参数
        if(empty($options)){
            $options = ['is_admin_log' => 0, 'is_admin_role' => 0,];
        }

        // 执行
        $res = $logic->edit($this->myUser, $options);
        if($res === false){
            return $this->error($logic->getError());
        }

        return $this->success();
    }

    /**
     * // 修改状态
     * @var  $logic \App\Manage\Manage
     */
    public function onStatus($logic, $options = [])
    {
        // 参数
        if(empty($options)){
            $options = ['is_admin_log' => 0, 'is_admin_role' => 0,];
        }

        // 执行
        $res = $logic->status($this->myUser, $options);
        if($res === false){
            return $this->error($logic->getError());
        }

        return $this->success();
    }


}


